package com.shaco.heroproject.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.shaco.heroproject.entity.Result_LatestNews;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/11/11.
 */
public class MySQLiteLatestNewsUtil {
    private SQLiteDatabase db;
    private Context context;

    public MySQLiteLatestNewsUtil(Context c) {
        // TODO Auto-generated constructor stub
        context = c;
        init();
    }

    private void init() {
        MySQLliteOpenHelper helper = new MySQLliteOpenHelper(context, Constant.DATABASE_NAME, null, 1);
        db = helper.getWritableDatabase();
    }
    public void saveDatasToDataBase(List<Result_LatestNews> results, String type) {
        for (Result_LatestNews result : results) {
            ContentValues values = new ContentValues();
            values.put(Constant.COLUMN_LATESTNEWS_COMMENT_COUNT, result.getComment_count());
            values.put(Constant.COLUMN_LATESTNEWS_ICON,result.getIcon());
            values.put(Constant.COLUMN_LATESTNEWS_ID,result.getId());
            values.put(Constant.COLUMN_LATESTNEWS_SOURCE,result.getSource());
            values.put(Constant.COLUMN_LATESTNEWS_TITLE,result.getTitle());
            values.put(Constant.COLUMN_LATESTNEWS_TYPE,type);
            db.insert(Constant.TABLE_NAME_LATESTNEWS, null, values);
        }
    }
    public List<Result_LatestNews> queryByType(String type) {
        String sql = " SELECT * FROM " + Constant.TABLE_NAME_LATESTNEWS;
        Cursor cursor = db.rawQuery(sql, null);
        if (cursor != null) {
            List<Result_LatestNews> results = new ArrayList<Result_LatestNews>();
            while (cursor.moveToNext()) {
                int comment_count=cursor.getInt(cursor.getColumnIndex(Constant.COLUMN_LATESTNEWS_COMMENT_COUNT));
                String icon=cursor.getString(cursor.getColumnIndex(Constant.COLUMN_LATESTNEWS_ICON));
                String id=cursor.getString(cursor.getColumnIndex(Constant.COLUMN_LATESTNEWS_ID));
                String source=cursor.getString(cursor.getColumnIndex(Constant.COLUMN_LATESTNEWS_SOURCE));
                String title=cursor.getString(cursor.getColumnIndex(Constant.COLUMN_LATESTNEWS_TITLE));

                Result_LatestNews result = new Result_LatestNews();
                result.setComment_count(comment_count);
                result.setIcon(icon);
                result.setId(id);
                result.setSource(source);
                result.setTitle(title);
                results.add(result);
            }
            cursor.close();
            return results;
        }
        return null;
    }
    public void deleteByTitle(int id) {
        String sql = " DELETE  FROM " + Constant.TABLE_NAME_ALLHERORESULT + " WHERE id = " + id;
        db.execSQL(sql);
    }

    public void deleteAll() {
        String sql = " DELETE FROM " + Constant.TABLE_NAME_ALLHERORESULT;
        db.execSQL(sql);
    }

}
